package com.liuheqiao.sushegaunli.dao;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.annotation.SuppressLint;

import com.liuheqiao.sushegaunli.model.Dormitory;
import com.liuheqiao.sushegaunli.db.DBHelper;

import java.util.ArrayList;
import java.util.List;

public class DormitoryDao {
    private DBHelper dbHelper;

    public DormitoryDao(DBHelper dbHelper) {
        this.dbHelper = dbHelper;
    }

    // 根据楼号和房间号获取宿舍信息
    @SuppressLint("Range")
    public Dormitory getDormitoryByNumber(String buildingNo, String roomNo) {
        // 添加空值检查
        if (buildingNo == null || roomNo == null) {
            return null;
        }

        SQLiteDatabase db = dbHelper.getReadableDatabase();
        Cursor cursor = db.query(DBHelper.TABLE_DORMITORIES,
                null,
                DBHelper.COLUMN_BUILDING_NO + " = ? AND " + DBHelper.COLUMN_ROOM_NO + " = ?",
                new String[]{buildingNo, roomNo},
                null, null, null);

        Dormitory dormitory = null;
        if (cursor != null && cursor.moveToFirst()) {
            dormitory = new Dormitory();
            dormitory.setId(cursor.getInt(cursor.getColumnIndex(DBHelper.COLUMN_ID)));
            dormitory.setBuildingNo(cursor.getString(cursor.getColumnIndex(DBHelper.COLUMN_BUILDING_NO)));
            dormitory.setRoomNo(cursor.getString(cursor.getColumnIndex(DBHelper.COLUMN_ROOM_NO)));
            dormitory.setCapacity(cursor.getInt(cursor.getColumnIndex(DBHelper.COLUMN_CAPACITY)));
            dormitory.setCurrentCount(cursor.getInt(cursor.getColumnIndex(DBHelper.COLUMN_CURRENT_COUNT)));
            dormitory.setDescription(cursor.getString(cursor.getColumnIndex(DBHelper.COLUMN_DESCRIPTION)));
            cursor.close();
        }
        return dormitory;
    }

    // 获取所有宿舍列表
    @SuppressLint("Range")
    public List<Dormitory> getAllDormitories() {
        List<Dormitory> dormitories = new ArrayList<>();
        SQLiteDatabase db = dbHelper.getReadableDatabase();
        Cursor cursor = db.query(DBHelper.TABLE_DORMITORIES,
                null, null, null, null, null, null);

        while (cursor.moveToNext()) {
            Dormitory dormitory = new Dormitory();
            dormitory.setId(cursor.getInt(cursor.getColumnIndex(DBHelper.COLUMN_ID)));
            dormitory.setBuildingNo(cursor.getString(cursor.getColumnIndex(DBHelper.COLUMN_BUILDING_NO)));
            dormitory.setRoomNo(cursor.getString(cursor.getColumnIndex(DBHelper.COLUMN_ROOM_NO)));
            dormitory.setCapacity(cursor.getInt(cursor.getColumnIndex(DBHelper.COLUMN_CAPACITY)));
            dormitory.setCurrentCount(cursor.getInt(cursor.getColumnIndex(DBHelper.COLUMN_CURRENT_COUNT)));
            dormitory.setDescription(cursor.getString(cursor.getColumnIndex(DBHelper.COLUMN_DESCRIPTION)));
            dormitories.add(dormitory);
        }
        cursor.close();
        return dormitories;
    }

    // 检查宿舍是否已满
    public boolean isDormitoryFull(String buildingNo, String roomNo) {
        Dormitory dormitory = getDormitoryByNumber(buildingNo, roomNo);
        return dormitory != null && dormitory.getCurrentCount() >= dormitory.getCapacity();
    }

    // 更新宿舍当前人数
    public boolean updateDormitoryCount(String buildingNo, String roomNo, int change) {
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        Dormitory dormitory = getDormitoryByNumber(buildingNo, roomNo);
        if (dormitory == null) return false;

        int newCount = dormitory.getCurrentCount() + change;
        if (newCount < 0 || newCount > dormitory.getCapacity()) return false;

        ContentValues values = new ContentValues();
        values.put(DBHelper.COLUMN_CURRENT_COUNT, newCount);

        int rows = db.update(DBHelper.TABLE_DORMITORIES,
                values,
                DBHelper.COLUMN_BUILDING_NO + " = ? AND " + DBHelper.COLUMN_ROOM_NO + " = ?",
                new String[]{buildingNo, roomNo});

        return rows > 0;
    }
} 